package me.blankm.websocket;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import me.blankm.wslib.WsManager;
import me.blankm.wslib.icallback.ICallback;
import me.blankm.wslib.common.WsStatus;
import me.blankm.wslib.icallback.WebSocketCallback;
import me.blankm.wslib.request.Request;
import me.blankm.wslib.utils.WsLibLogUtils;

/**
 * Author by Mr.Meng
 * created 2021/11/24 16:22
 *
 * @desc
 */

public class WsUtils implements WebSocketCallback {
    private final String TAG = this.getClass().getSimpleName();


    private WsUtils() {

    }

    private static class WsUtilsHolder {
        private static final WsUtils mInstance = new WsUtils();
    }

    public static WsUtils getInstance() {
        return WsUtilsHolder.mInstance;
    }


    /*连接socket*/
    public void onConnect() {
        WsManager.getInstance().setWsCallback(this);
        WsManager.getInstance().onConnect();
    }

    @Override
    public void onLoginAuth() {
        //登录socket授权 ，因为此处不需要登录，我们直接以登陆成功，执行下一步操作
        WsManager.getInstance().setStatus(WsStatus.AUTH_SUCCESS);
        //开始保活（执行心跳）
        WsManager.getInstance().startKeepAlive();
    }

    @Override
    public void sendHeartEvent() {
        //发送心跳



    }

    @Override
    public void onReceivedMessage(String msg) {
        //收到来自服务端的信息

    }




    public void onEnterRoom() {
        Map<String, String> args = new HashMap<>();
        args.put("user_id", "10000038");
        args.put("room_id", "1920");
        args.put("user_id", "10000038");
        args.put("token", "58d1b088-545b-42d6-b6a7-9f533d405eac");
        args.put("app_version", "400");
        args.put("udid", "2198f3422d05936c4822f1e067e029caa");
        args.put("platform", "android");

    }


    private String jsonToString(String mod, String act, Map<String, String> map) {
        JSONObject object = new JSONObject();
        JSONObject objArgs = new JSONObject();
        try {
            object.put("mod", mod);
            object.put("act", act);
            if (map != null) {
                //args对象参数
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    objArgs.put(entry.getKey(), entry.getValue());
                }
            }
            objArgs.put("user_id", "10000038");
            objArgs.put("token", "58d1b088-545b-42d6-b6a7-9f533d405eac");
            objArgs.put("ck", "FWZ4QbhJe0377cCk");
            objArgs.put("app_version", "400");
            objArgs.put("udid", "2198f3422d05936c4822f1e067e029caa");
            objArgs.put("platform", "android");
            object.put("args", objArgs);
            WsLibLogUtils.i(TAG, "send Message=>" + object.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return object.toString();
    }

}
